﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LIMS.Entities;
using LIMS.DataAccess;
using System.Collections;
using System.Reflection;
using Castle.Core.Logging;
using NHibernate.Criterion;

namespace LIMS.Service
{
    public class SiteManagementService : ISiteManagementService
    {
        //Private DBA interfaces
        private IDaoFactory oDBFactory;
        private ISiteDAO oSiteDBA;
        private ILogger logger;

        //Constructor
        public SiteManagementService(IDaoFactory oDBOp, ILogger oLogger)
        {
            this.oDBFactory = oDBOp;
            this.oSiteDBA = oDBFactory.GetSiteDAO();
            this.logger = oLogger;
        }
        #region ISiteManagementService Members
       
        /// <summary>
        /// Save
        /// </summary>
        /// <param name="newSite"></param>
        /// <returns></returns>
        public bool Save(Site newSite)
        {
            return oSiteDBA.QuickSaveOrUpdate(ref newSite);
        }
        /// <summary>
        /// Delete
        /// </summary>
        /// <param name="dSite"></param>
        /// <returns></returns>
        public bool Delete(Site dSite)
        {
            return oSiteDBA.QuickDelete(dSite);
        }

        /// <summary>
        /// GetAllSitesForGrid
        /// </summary>
        /// <param name="oGrdData"></param>
        /// <returns></returns>

        public List<Site> GetAllSites()
        {
            List<Site> oAllSites = new List<Site>();
            try
            {
                //return oCountiesDBA.GetAll();
                ICriterion oCriteriaStatus = Expression.Eq("Status", Resource.ACTIVE_STATUS);
                return oSiteDBA.GetByCriteria(oCriteriaStatus);
            }
            catch (Exception ex)
            {
                logger.Error("Error in Getting Sites ", ex);
            }
            return oAllSites;
        }

        /// <summary>
        /// GetAllSitesForGrid
        /// </summary>
        /// <param name="oGrdData"></param>
        /// <returns></returns>
        public List<Site> GetAllSitesForGrid(ref GridViewData oGrdData)
        {
            return oSiteDBA.GetAllForGridView(ref oGrdData);
        }
        /// <summary>
        /// GetSiteNameByID
        /// </summary>
        /// <param name="siteID"></param>
        /// <returns></returns>
        public string GetSiteNameByID(int siteID)
        {
            string siteName = string.Empty;
            List<Site> oSiteList = new List<Site>();
            try
            {
                ICriterion oCriteriaStatus = Expression.Eq("Status", Resource.ACTIVE_STATUS);
                ICriterion oCriteriaID = Expression.Eq("Id", siteID);
                ICriterion oFinalCriteria = Expression.And(oCriteriaStatus, oCriteriaID);
                oSiteList = oSiteDBA.GetByCriteria(oFinalCriteria);
                if (oSiteList.Count > 0)
                {
                    foreach (Site oSite in oSiteList)
                    {
                        siteName = oSite.Name;
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return siteName;
        }


        #endregion
    }
}
